package com.loto.insurance.mis.web.authority;

import com.loto.insurance.mis.web.entity.config.ChildMenu;
import com.loto.insurance.mis.web.entity.config.Menu;
import com.loto.insurance.mis.web.entity.config.MenuConfig;
import com.loto.insurance.mis.web.enumeration.AuthorityRoleEnum;
import com.loto.insurance.mis.web.enumeration.MenuEnum;
import com.loto.insurance.mis.web.utils.Config;

public class AuthorityHelper {

  public static boolean hasAuthority(String userRoleIds,
	  AuthorityRoleEnum roleId, String userName, MenuEnum menuId) {
	boolean isAuthoried = false;
	if (AuthorityRoleEnum.NOT_SPECIFIED == roleId)
	  isAuthoried = true;
	else {
	  String[] roles = userRoleIds.split(",");
	  for (String role : roles) {
		if (role == String.valueOf(roleId.getIndex())) {
		  isAuthoried = true;
		  break;
		}
	  }
	}
	if (isAuthoried && !menuId.equals(MenuEnum.NOT_SPECIFIED)) {
	  isAuthoried = false;
	  Config config = new Config();
	  MenuConfig menuConfig = config.getMenuConfigFile(userName);
	  if (menuConfig != null && menuConfig.getMenuList().size() > 0) {
		for (Menu menu : menuConfig.getMenuList()) {
		  boolean isMatched = false;
		  for (ChildMenu child : menu.getChildMenuList()) {
			if (child.getId() == menuId.getIndex()) {
			  isMatched = true;
			  break;
			}
		  }
		  if (isMatched) {
			isAuthoried = true;
			break;
		  }
		}
	  }
	}
	return isAuthoried;
  }
};
